package com.lkd.handler;


import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.VendoutResultContract;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_RESULT_TOPIC)
public class VendoutResultMsgHandle implements MsgHandler {

    @Autowired
    private OrderService orderService;
    /**
     * 订单微服务接收售货机终端消息，处理出货结果
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        VendoutResultContract vendoutResultContract = JsonUtil.getByJson(jsonMsg, VendoutResultContract.class);
        OrderEntity orderEntity = orderService.getByOrderNo(vendoutResultContract.getVendoutData().getOrderNo());
        if (orderEntity==null)return;
        if (vendoutResultContract.isSuccess()){
            //出货成功，修改订单状态为出货成功
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_SUCCESS);//出货成功
        }else {
            //出货失败，修改订单状态为出货失败，并启动退款流程
            orderEntity.setStatus(VMSystem.ORDER_STATUS_VENDOUT_FAIL);
            //todo:启动退款流程
        }
        orderService.updateById(orderEntity);//更新出货成功或者失败的状态
    }
}
